<?php

/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/10/10
 * Time: 16:01
 */

namespace app\api\controller;


use app\api\BaseController;
use app\common\model\Inviter;
use app\common\model\Search;
use app\common\model\Storeplate;
use app\common\model\Transport;
use app\service\GoodsService;
use app\common\model\Goodproject;
use app\common\model\Goodsclass;
use app\common\model\Brand;
use app\common\model\Pxianshigoods;
use app\common\model\Refundreturn;
use think\Request;
use think\Db;

/**
 * @title 商品
 * @description 商品相关
 */
class Goods extends BaseController
{
    private $_model_search;
    const PAGESIZE = 150;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->_model_search = new Search();
    }

    /**
     * @title 商品分类
     * @description
     * @param string none none ''
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /goods/goodsClass
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function goodsClass(\app\common\model\Goodsclass $goodsClass, $parent_id = 0)
    {
        $goods_class_array = $goodsClass->getGoodsclassListByParentId($parent_id);

        json_success_return($goods_class_array);
    }

    /**
     * @title 商品分类
     * @description
     * @param string none none ''
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /goods/goodsClass
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function getgoodsClass()
    {

        $gc_list = db('goodsclass')->where('gc_show', '1')->select();

        json_success_return($gc_list);
    }

    /**
     * @title 商品列表
     * @description
     * @param string page 页数 1 false
     * @param string keyword 搜索内容 '' false
     * @param string order 排序内容(1升序2降序) 2 false
     * @param string key 排序方式(0销量2人气3价格) 0 false
     * @param string cate_id 分类id '' false
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /goods/goodsList
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function goodsList(\app\common\model\Goods $goods_model)
    {

        $cate_id = $default_classid = intval(input('param.cate_id'));
        $item_number = intval(input('param.item_number'));
        $keyword = input('param.keyword');

        //优先从全文索引库里查找
        //        list($indexer_ids, $indexer_count) = $this->_model_search->indexerSearch(input('param.'), self::PAGESIZE);

        //获得经过属性过滤的商品信息
        list($goods_param, $brand_array, $initial_array, $attr_array, $checked_brand, $checked_attr) = $this->_model_search->getAttribute(input('param.'), $default_classid);

        //处理排序
        $key = input('param.key');
        $order = input('param.order');

        #判断是否有升序，降序
        if (empty($order)) {
            $order = 'goodscommon.mall_goods_sort desc';
        } else {
            switch ($order) {
                case 1:
                    #升序
                    $order = 'goods.goods_salenum asc';
                    break;
                case 2:
                    #降序
                    $order = 'goods.goods_salenum desc';
                    break;
                default:
                    if (in_array($key, array('1', '2', '3'))) {
                        $sequence = $order == '1' ? 'asc' : 'desc';
                        $order = str_replace(array('1', '2', '3'), array('goods.goods_salenum', 'goods.goods_click'), $key);
                        $order .= ' ' . $sequence;
                    }
                    break;
            }
        }

        // 字段
        $fields = "goods.goods_id,goods.markup_price,goods.ti_final_price,goods.gu_final_price,goodscommon.brand_name,goodscommon.goods_commonid,goodscommon.goods_name,goodscommon.gc_id,goodscommon.store_id,goodscommon.store_name,goodscommon.goods_price,goods.goods_promotion_price,goods.goods_promotion_type,goodscommon.goods_marketprice,goods.goods_storage,goodscommon.goods_image,goodscommon.goods_freight,goods.goods_salenum,goods.color_id,goods.evaluation_good_star,goods.evaluation_count,goodscommon.is_virtual,goodscommon.is_goodsfcode,goodscommon.is_appoint,goodscommon.is_presell,goods.is_have_gift";
        // $fields = '*';
        $condition = array();
        //        if (is_array($indexer_ids)) {
        //
        //            //商品主键搜索
        //            $condition['goods_id'] = array('in', $indexer_ids);
        //            $goods_list = $goods_model->getGoodsOnlineList($condition, $fields, 0, $order, self::PAGESIZE, null, false);
        //
        //            //如果有商品下架等情况，则删除下架商品的搜索索引信息
        //            if (count($goods_list) != count($indexer_ids)) {
        //                $this->_model_search->delInvalidGoods($goods_list, $indexer_ids);
        //            }
        //
        //        } else {
        //执行正常搜索

        #shen 12.23  直销 回去父级store_id
        $store_id = get_parent_store_id();
        $condition['y_goods.store_id'] = $store_id;
        $condition['y_goods.status'] = 0;
        $condition['y_goods.is_del'] = 0;

        if (isset($goods_param['class']['depth'])) {
            $condition['goodscommon.gc_id_' . $goods_param['class']['depth']] = $goods_param['class']['gc_id'];
        }
        #品牌搜索
        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $condition['goodscommon.brand_id'] = $b_id;
        }
        #分类搜索
        if ($cate_id > 0) {
            $condition['goodscommon.gc_id'] = $cate_id;
        }
        #货号搜索
        if ($item_number > 0) {
            $condition['goodscommon.goods_commonid'] = $item_number;
        }
        if ($keyword != '') {
            $condition['goodscommon.goods_name|goodscommon.goods_advword'] = array('like', '%' . $keyword . '%');
        }
        $area_id = intval(input('param.area_id'));
        if ($area_id > 0) {
            $condition['goodscommon.areaid_1'] = $area_id;
        }

        $type = intval(input('param.type'));
        if ($type == 1) {
            $condition['goodscommon.is_platform_store'] = 1;
        }
        $gift = intval(input('param.gift'));
        if ($gift == 1) {
            $condition['goods.is_have_gift'] = 1;
        }
        if (isset($goods_param['goodsid_array'])) {
            $condition['goods.goods_id'] = array('in', $goods_param['goodsid_array']);
        }
        $priceMin = intval(input('param.priceMin'));
        if ($priceMin > 0) {
            $condition['goodscommon.goods_price'] = array('egt', $priceMin);
        }
        $priceMax = intval(input('param.priceMax'));
        if ($priceMax > 0) {
            $condition['goodscommon.goods_price'] = array('elt', $priceMax);
        }

        if ($priceMin > 0 && $priceMax > 0) {
            $condition['goodscommon.goods_price'] = array('between', array($priceMin, $priceMax));
        }

        $discount_id = input('param.discount_id'); #折扣id
        if (!empty($discount_id)) {
            //获取折扣详情
            $discount_info = db('pxianshi')->alias('p')
                ->join('pxianshigoods pg', 'pg.xianshi_id = p.xianshi_id')
                ->where(['p.xianshi_id' => $discount_id])
                ->select();
            $goods_array = [];
            foreach ($discount_info as $k => $v) {
                $goods_array[] = $v['goods_id'];
            }
            $goods_ids = implode(',', $goods_array);
            $condition['goods.goods_id'] = array('in', $goods_ids);
        }

        $goods_list = $goods_model->getGoodsUnionList($condition, $fields, $order, 'goodscommon.goods_commonid', self::PAGESIZE);

        $supplier_type = get_supplier_type();
        if ($supplier_type == 1) {
            #加价
            foreach ($goods_list as $k => $v) {
                $y_goods_info = db('y_goods')->where('store_id', $store_id)->where('goods_id', $v['goods_id'])->find();
                //p($y_goods_info);die;
                $goods_list[$k]['final_price'] = (float)$goods_list[$k]['gu_final_price'] + (float)$y_goods_info['price'];
            }
        } else {
            #按照提成走
            foreach ($goods_list as $k => $v) {
                $goods_list[$k]['final_price'] = $goods_list[$k]['ti_final_price'];
            }
        }
        //        }

        #价格排序
        $is_price = input('is_price_sort', '');
        if (!empty($is_price)) {
            #1:升序 2：降序
            $last_names = array_column($goods_list, 'final_price');
            if ($is_price == 1) {
                array_multisort($last_names, SORT_ASC, $goods_list);
            } else {
                array_multisort($last_names, SORT_DESC, $goods_list);
            }
        }


        $this->assign('show_page', is_object($goods_model->page_info) ? $goods_model->page_info->render() : "");

        // 商品多图
        if (!empty($goods_list)) {
            $commonid_array = array(); // 商品公共id数组
            $storeid_array = array();       // 店铺id数组
            foreach ($goods_list as $value) {
                $commonid_array[] = $value['goods_commonid'];
                $storeid_array[] = $value['store_id'];
            }
            $commonid_array = array_unique($commonid_array);
            $storeid_array = array_unique($storeid_array);

            // 商品多图
            $goodsimage_more = $goods_model->getGoodsImageList(array('goods_commonid' => array('in', $commonid_array)));

            // 店铺
            $store_list = model('store')->getStoreMemberIDList($storeid_array);
            //搜索的关键字
            $search_keyword = $keyword;
            foreach ($goods_list as $key => &$value) {
                // 商品多图
                //商品列表主图限制不越过5个
                $value['goods_image'] = goods_thumb($value);
                $n = 0;
                foreach ($goodsimage_more as &$v) {
                    $v['goodsimage_url'] = goods_thumb($v);

                    if ($value['goods_commonid'] == $v['goods_commonid'] && $value['store_id'] == $v['store_id'] && $value['color_id'] == $v['color_id']) {
                        $n++;
                        $goods_list[$key]['image'][] = $v;
                        if ($n >= 5)
                            break;
                    }
                }
                // 店铺的开店会员编号
                $store_id = $value['store_id'];
                $goods_list[$key]['member_id'] = $store_list[$store_id]['member_id'];
                //将关键字置红
                if ($search_keyword) {
                    $goods_list[$key]['goods_name_highlight'] = str_replace($search_keyword, '<font style="color:#f00;">' . $search_keyword . '</font>', $value['goods_name']);
                } else {
                    $goods_list[$key]['goods_name_highlight'] = $value['goods_name'];
                }
                /* $value['goods_price'] = GoodsService::getInstance()->calculatePrice(get_supplier_type(),$value);*/
                $value['goods_price'] = 0;
            }
        }
        //p($goods_list);die;
        json_success_return($goods_list);
    }

    /**
     * @title 商品详情
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author fuzz
     * @url /goods/detail
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function detail()
    {
        $goods_id = intval(input('param.goods_id'));

        $goods_commonid = intval(input('param.goods_commonid'));
        $goods_name = input('param.goods_name');

        $goods_model = new \app\common\model\Goods();
        if (!empty($goods_commonid) && !empty($goods_commonid)) {
            #切换规格
            if (empty($goods_id)) {
                #没传的话是切换规格
                $new_array = explode(" ", $goods_name);
                unset($new_array[0]);
                $goods_name = implode(" ", $new_array);
            }


            $goods_detail = $goods_model->getGoodsname_Detail($goods_commonid, $goods_name);
            $goods_info = $goods_detail['goods_info'];

            if ($goods_info['goods_storage'] == 0) {

                return json_fail_return('暂时无货');
            }

        } else {

            #默认第一次进入

            // 商品详细信息
            $goods_detail = $goods_model->getGoodsDetail($goods_id);
           // p($goods_detail);die;
            $goods_info = $goods_detail['goods_info'];
            if(empty($goods_info)){
                return json_fail_return('商品已下架');
            }
        }
        //p($goods_info);die;

        #拼接品牌名
        $new_brand_info = db('brand')->where(['brand_id' => $goods_info['brand_id']])->find();
        if (!empty($new_brand_info)) {
            $goods_info['goods_name'] = $new_brand_info['brand_name'] . ' ' . $goods_info['goods_name'];
        }

        if (empty($goods_info)) {
            return json_fail_return('商品已下架或不存在');
        }
        $goods_info['spec_list'] = $goods_detail['spec_list'];
        // 获取销量 BEGIN
        $rs = $goods_model->getGoodsList(array('goods_commonid' => $goods_info['goods_commonid']));
        $count = 0;
        foreach ($rs as $v) {
            $count += $v['goods_salenum'];
        }
        $goods_info['goods_salenum'] = $count;



        /*$other = '1|2|3';
        ['1|1|1','1|2|3']
        $other_array= explode('|',$other);
        for($i=0;$i<count($other_array)-1;$i++){
            $new_id=$other_array[$i];

        }*/


        // 如果使用售卖区域
        if ($goods_info['transport_id'] > 0) {
            // 取得三种运送方式默认运费
            $transport_model = new Transport();
            $transport = $transport_model->getTransportextendList(array('transport_id' => $goods_info['transport_id'], 'transportext_is_default' => 1));
            if (!empty($transport) && is_array($transport)) {
                foreach ($transport as $v) {
                    $goods_info["transport_price"] = $v['transportext_sprice'];
                }
            }
        }
        $inviter_model = new Inviter();
        $goods_info['inviter_amount'] = 0;
        if (config('inviter_show') && config('inviter_open') && $goods_info['inviter_open'] && $this->user_id && $inviter_model->getInviterInfo('i.inviter_id=' . $this->user_id . ' AND i.inviter_state=1')) {
            $inviter_amount = round($goods_info['inviter_ratio_1'] / 100 * $goods_info['goods_price'], 2);
            if ($inviter_amount > 0) {
                $goods_info['inviter_amount'] = $inviter_amount;
            }
        }
        // halt($goods_info);
        $this->assign('goods', $goods_info);

        $storeplate_model = new Storeplate();
       /* // 顶部关联版式
        if ($goods_info['plateid_top'] > 0) {
            $plate_top = $storeplate_model->getStoreplateInfoByID($goods_info['plateid_top']);
            $goods_info['plate_top'] = $plate_top;
        }
        // 底部关联版式
        if ($goods_info['plateid_bottom'] > 0) {
            $plate_bottom = $storeplate_model->getStoreplateInfoByID($goods_info['plateid_bottom']);
            $goods_info['plate_bottom'] = $plate_bottom;
        }*/

        $goods_info['goods_image'] = goods_thumb($goods_info);

        $commonid_array[] = $goods_info['goods_commonid'];
        $storeid_array[] = $goods_info['store_id'];
        $commonid_array = array_unique($commonid_array);
        $storeid_array = array_unique($storeid_array);

        // 商品多图
        $goodsimage_more = $goods_model->getGoodsImageList(array('goods_commonid' => array('in', $commonid_array)));

        //商品列表主图限制不越过5个
        $n = 0;
        foreach ($goodsimage_more as &$v) {
            $config = db('config')->where('code', 'qiniu_http')->find();

            $v['goodsimage_url'] = $config['value'] . '/' . $v['goodsimage_url'];

            // $v['goodsimage_url'] = goods_thumb($v);

            if ($goods_info['goods_commonid'] == $v['goods_commonid'] && $goods_info['store_id'] == $v['store_id']) {
                $n++;
                $goods_info['image'][] = $v;
                if ($n >= 5)
                    break;
            }
        }
        // 店铺的开店会员编

        //评价信息
        $goods_evaluate_info = model('evaluategoods')->getEvaluategoodsInfoByGoodsID($goods_id);
        $this->assign('goods_evaluate_info', $goods_evaluate_info);
        $goods_info['goods_evaluate_info'] = $goods_evaluate_info;

        //返回默认地址信息
        $token = input('token', '');
        $token_info = db('token')->where('token', $token)->find();
        $user_info = db('member')->where('member_id', $token_info['user_id'])->find();
        $deault_address_info = db('address')->where('member_id', $user_info['member_id'])->where('address_is_default', '1')->find();
        if (empty($deault_address_info)) {
            $goods_info['default_address'] = (object)array();
        } else {
            $goods_info['default_address'] = $deault_address_info;
        }


        if (empty($goods_info['videos'])) {
            $goods_info['videos'] = '';
        } else {
            $config = db('config')->where('code', 'qiniu_http')->find();
            $goods_info['videos'] = $config['value'] . '/' . $goods_info['videos'];
        }
        if (empty($goods_info['goods_body'])) {
            $goods_info['goods_body'] = '';
        } else {
            $goods_info['goods_body'] = htmlspecialchars_decode($goods_info['goods_body']);
        }
        #说明书
        if (!empty($goods_info['files'])) {
            $goods_info['files_info'] = json_decode($goods_info['files'], true);
            $goods_info['files_info']['old_file_name'] = explode(',', $goods_info['files_info']['old_file_name']);
            $goods_info['files_info']['new_file_name'] = explode(',', $goods_info['files_info']['new_file_name']);

            $config = db('config')->where('code', 'qiniu_http')->find();
            foreach ($goods_info['files_info']['new_file_name'] as $k => $v) {
                $goods_info['files_info']['new_file_name'][$k] = $config['value'] . '/' . $v;
            }
        } else {
            $goods_info['files_info'] = (object)array();
        }

        $token = input('token');
        $token_info = db('token')->where('token', $token)->find();

        //判断商品是否收藏
        $favorite_info = db('favorites')->where(['fav_id' => $goods_id, 'fav_type' => 1, 'member_id' => $token_info['user_id']])->find();
        if (empty($favorite_info)) {
            //收藏表里没数据  0:未收藏 1:已收藏
            $goods_info['favorite_status'] = 0;
        } else {
            if ($favorite_info['status'] == 0) {
                //当商品是收藏时
                $goods_info['favorite_status'] = 1;
            } else {
                $goods_info['favorite_status'] = 0;
            }
        }

        //浏览记录
        #首先查看记录表里面有没有当前商品的信息
        $store = db('token')->where(['token' => input('post.token')])->find();
        #判断是否是二手
        if ($goods_info['is_two_goods'] == 0) {
            $record_type = 1;
        } else {
            $record_type = 1;
        }
        $record_where = [
            'type' => $record_type,
            'member_id' => $store['user_id'],
            'goods_id' => $goods_id,
        ];
        $record_info = db('record')->where($record_where)->find();
        if (!empty($record_info)) {
            #如果有记录，更新时间和浏览次数
            $new_record['browse_nums'] = $record_info['browse_nums'] + 1;
            $new_record['add_time'] = time();
            db('record')->where($record_where)->update($new_record);
        } else {
            #没有记录新增
            $record_data = [
                'type' => $record_type,
                'member_id' => $store['user_id'],
                'goods_id' => $goods_id,
                'goods_name' => $goods_info['goods_name'],
                'goods_price' => $goods_info['goods_price'],
                'goods_images' => $goods_info['goods_image'],
                'goods_commonid' => $goods_info['goods_commonid'],
                'add_time' => time()
            ];
            db('record')->insertGetId($record_data);
        }
        return json_success_return($goods_info);
    }


    /**
     * Notes:获取商品安装费 34000
     * User: shen
     * Date: 2020/4/30
     * Time: 14:40
     */
    public function get_install_money()
    {

        $return_data = [
            'install_money' => 0
        ];

        $address_id = intval(input('address_id'));
        /* if (!$address_id) {
             return json_fail_return('缺少地址');
         }*/

        $goods_id = intval(input('goods_id'));
        if (!$goods_id) {
            return json_fail_return('缺少商品id');
        }

        $token = input('token');
        $token_info = db('token')->where('token', $token)->find();
        $user_id = $token_info['user_id'];

        $goods_info = db('goods')->where('goods_id', $goods_id)->find();
        if ($address_id) {
            $address_info = db('address')->where(['member_id' => $user_id, 'address_id' => $address_id])->find();
            if (!$address_info) {
                return json_fail_return('地址不存在');
            }
            if ($goods_info['install_money']) {
                $return_data['install_money'] = get_install_money($address_info['area_id'], (float)$goods_info['install_money']);
            } else {
                $return_data['install_money'] = 0;
            }
        } else {
            if ($goods_info['install_money']) {
                $return_data['install_money'] = get_install_money(0, (float)$goods_info['install_money']);
            } else {
                $return_data['install_money'] = 0;
            }
        }


        return json_success_return($return_data, '获取成功');


    }


    /**
     * @title 主分类获取品牌接口
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/goods_collection       favorites
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_main_class_brand()
    {
        $class_id = input('post.class_id');
        if (empty($class_id)) {
            return json_fail_return('参数错误');
        }
        $result = db('brand')->where(['gc_id' => $class_id])->select();
        return json_success_return($result);
    }

    /**
     * @title 判断销售区域
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/goods_collection       favorites
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function get_sale_area()
    {
        $goods_id = input('post.goods_id');
        $address_id = input('post.address_id');
        if (empty($goods_id) || empty($address_id)) {
            return json_fail_return('参数错误');
        }
        //获取用户地址的所在位置的区code
        $address_info = db('address')->where(['address_id' => $address_id])->find();

        $goods_info = db('goods')->where(['goods_id' => $goods_id])->find();

        if (strstr($goods_info['area'], (string)$address_info['area_id'])) {
            return json_success_return([
                'type' => 1,
            ]);
        } else {
            return json_success_return([
                'type' => 0,
                'msg' => '该商品不在可销售区域内'
            ]);
        }
    }


    /**
     * @title 商品收藏
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/goods_collection       favorites
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function goods_collection()
    {
        $store_id = $this->user_id;
        $status = input('post.status');
        if (empty($store_id)) {
            return json_fail_return('参数错误');
        }
        $GoodprojectModel = new Goodproject();
        $result = $GoodprojectModel->getCollection($store_id, $status);
        return json_success_return($result);
    }


    /**
     * @title 获取项目定制分类
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/project_class
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function project_class()
    {
        $GoodprojectModel = new Goodproject();
        $result = $GoodprojectModel->getProjectListClass();
        return json_success_return($result);
    }

    /**
     * @title 获取项目定制列表
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/project
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function project_list()
    {
        $project_title = input('post.project_title');
        $project_class = input('post.project_class');
        $GoodprojectModel = new Goodproject();
        $result = $GoodprojectModel->getProjectList($project_title, $project_class);
        return json_success_return($result);
    }

    /**
     * @title 获取项目定制详情
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/project_info
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function project_info()
    {
        $project_id = input('post.project_id');
        if (empty($project_id)) {
            return json_fail_return('参数错误');
        }
        $GoodprojectModel = new Goodproject();

        $store = db('token')->where(['token' => input('post.token')])->find();

        $result = $GoodprojectModel->getProjectInfo($project_id, $store['user_id']);
        return json_success_return($result);
    }

    /**
     * @title 搜索商品名称  （搜设备库里面的商品）
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/search_goods_name
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function search_goods_name()
    {
        $data = input('post.');
        if ($name = input('goods_name')) {
            $condition['goods_name'] = array('like', "%" . $name . "%");
        }

        $condition['equipment_del'] = 0;
        $condition['equipment_status'] = 1;
        $equipmentModel = new \app\common\model\Equipment();
        $equipment_list = $equipmentModel->getNquipmentList($condition, 10);

        return json_success_return($equipment_list);
    }

    //二手设备根据分类名称获取相应的品牌
    public function search_goods_brand()
    {
        $data = input('post.');
        if (empty($data['equipment_id'])) {
            return json_fail_return('参数错误');
        }

        #获取设备库详情
        $equipment_info = db('new_equipment')->where(['id' => $data['equipment_id']])->find();

        // 转为数组遍历查下面的品牌
        $result = [];
        if (!empty($equipment_info['equipment_band_id'])) {
            $brand_array = explode(",", $equipment_info['equipment_band_id']);
            foreach ($brand_array as $key => $value) {
                $res = db('new_equipment_brand')
                    ->where(['equipment_brand_id' => $value])
                    ->field('equipment_brand_id,brand_name,brand_id,equipment_id')
                    ->find();
                if ($res) {
                    $result[] = $res;
                }
            }
        }
        return json_success_return($result);
    }


    /**
     * @title 发布二手设备
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/two_goods_add
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function two_goods_add()
    {
        $goods_model = new \app\common\model\Goods();
        $data = input('post.');
        $store = db('token')->where(['token' => $data['token']])->find();
        $result = $goods_model->twoGoodsAdd($data, $store['user_id']);
        return json_success_return($result);
    }

    /**
     * @title 编辑二手设备
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/two_goods_add
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function two_goods_edit()
    {
        $goods_model = new \app\common\model\Goods();
        $data = input('post.');
        $store = db('token')->where(['token' => $data['token']])->find();
        $result = $goods_model->twoGoodsEdit($data, $store['user_id']);
        return json_success_return($result);
    }


    /**
     * @title 获取在售的二手设备列表
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/project
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_detice_list()
    {
        $goods_model = new \app\common\model\Goods();
        $goods_class = input('post.goods_class');
        $goods_name = input('post.goods_name');
        $result = $goods_model->getTwoList($goods_class, $goods_name);
        return json_success_return($result);
    }

    /**
     * @title 获取用户的二手设备详情
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/project
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_detice_info()
    {
        $goods_model = new \app\common\model\Goods();
        $commonid = input('post.commonid');
        $token = input('post.token');
        $member_info = db('token')->where(['token' => $token])->find();
        if (empty($commonid)) {
            return json_fail_return('参数错误');
        }
        $result = $goods_model->getTwoInfo($commonid, $member_info['user_id']);
        return json_success_return($result);
    }

    /**
     * @title 获取某个用户的二手设备列表
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/project
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function get_member_two_list()
    {
        $goods_model = new \app\common\model\Goods();
        $data = input('post.');
        $member_info = db('token')->where(['token' => $data['token']])->find();

        if (empty($member_info)) {
            return json_fail_return('参数错误');
        }
        $result = $goods_model->getMemberTwoList($data, $member_info['user_id']);
        return json_success_return($result);
    }

    /**
     * @title 用户删除、取消发布二手设备
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/project
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function get_cancel_two_goods()
    {
        $goods_model = new \app\common\model\Goods();
        $data = input('post.');
        $member_info = db('token')->where(['token' => $data['token']])->find();

        if (empty($member_info) || empty($data['type'])) {
            return json_fail_return('参数错误');
        }
        $result = $goods_model->getCancelTwoGoods($member_info['user_id'], $data['goodscommon_id'], $data['type']);
        return json_success_return($result);
    }

    /**
     * @title 二手设备获取一级分类
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_one_class()
    {
        $GoodsclassModel = new Goodsclass();
        $result = $GoodsclassModel->getOneClass();
        return json_success_return($result);
    }

    /**
     * @title 二手设备获取二级、三级分类
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_two_class()
    {
        $class_id = input('post.class_id');
        if (empty($class_id)) {
            return json_fail_return('参数错误');
        }
        $GoodsclassModel = new Goodsclass();
        $result = $GoodsclassModel->getTwoClass($class_id);
        return json_success_return($result);
    }

    /**
     * @title 二手设备获取品牌
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_brand()
    {
        $BrandModel = new Brand();
        $where = [
            'brand_apply' => 1
        ];
        $result = $BrandModel->getBrandList($where);
        return json_success_return($result);
    }

    /**
     * @title 发布二手设备
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function add_brand()
    {
        $BrandModel = new Brand();
        $data = input('post.');
        $result = $BrandModel->add_brand($data);
        return json_success_return($result);
    }

    /**
     * @title 折扣、满减信息
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/get_discount_info
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_discount_info()
    {
        $PxianshigoodsModel = new Pxianshigoods();
        $goods_id = intval(input('post.goods_id'));
        if (empty($goods_id)) {
            return json_fail_return('参数错误');
        }
        $result = $PxianshigoodsModel->getDiscountInfo($goods_id);
        return json_success_return($result);
    }

    /**
     * @title 获取退换货接口信息
     * @description
     * @param string goods_id 商品id null true
     * @return {"code":200,"msg":"succeess"}
     * *@author zjw
     * @url /goods/get_return_reason
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     */

    public function get_return_reason()
    {
        $RefundreturnModel = new Refundreturn();
        $type = intval(input('post.type'));
        if (empty($type)) {
            return json_fail_return('参数错误');
        }
        $result = $RefundreturnModel->getReturnReason($type);
        return json_success_return($result);
    }

    /**
     * 教程详情
     */
    public function Ziliaodetail()
    {
        if (empty($detailid = intval(input('detailid')))) {
            return json_fail_return('请正确输入参数');
        }
        $qu = intval(input('son')) ? intval(input('son')) : 0;

        $where['id'] = $detailid;
        $where['is_del'] = 0;

        if ($qu == 0) {
            $new_where['course_id'] = $detailid;

            $new_where['userid'] = $this->user_id;

            $course_info = db('course_user_collect')->where($new_where)->find();

            #判断是否收藏
            if (!empty($course_info)) {
                $detail = Db::name('course')->alias('c')
                    ->join('course_user_collect cuc', 'cuc.course_id=c.id')
                    ->where($new_where)
                    ->field('c.id,title,add_time,content,img,brand_name,gc_name,c.status,cuc.status as qu_status')
                    ->find();
                $course_id = $detail['id'];
            } else {
                $detail = Db::name('course')
                    ->where($where)
                    ->find();
                $detail['qu_status'] = 0;
                $course_id = $detail['id'];
            }
            if ($detail['status'] == 1) {
                return json_fail_return('当前资料库未启用');
            }

        } else {
            $detail = Db::name('course_info')
                ->where(['id' => $detailid, 'is_del' => 0])
                ->field('id,title,add_time,content,video,status,course_id')
                ->find();
            $detail['content'] = html_entity_decode($detail['content']);
            $course_id = $detail['course_id'];
        }

        $file_http = db('config')->where('code', 'qiniu_http')->find();
        if (isset($detail['img'])) {
            $detail['img'] = $file_http['value'] . '/' . $detail['img'];
        }
        if (isset($detail['video'])) {
            $detail['video'] = $file_http['value'] . '/' . $detail['video'];
        }
        $detail['add_time'] = date('Y-m-d', $detail['add_time']);
        $res = Db::name('course_info')
            ->where(['course_id' => $course_id, 'status' => 1, 'is_del' => 0])->field('id,title,qu')
            ->order('x_id asc')
            ->select();
        $detail['ziArr'] = $res;
        return json_success_return($detail);
    }

    public function getTypes()
    {
        $model = model('goodsclass');
        $result = $model->getOneClass('gc_id,gc_name', 'gc_sort asc');
        foreach ($result as $key => $value) {
            $result[$key]['child'] = $result2 = $model->getTwoClass($value['gc_id'], 'gc_id,gc_name', 'gc_sort asc');
            foreach ($result2 as $k => $v) {
                $result[$key]['child'][$k]['child'] = $model->getTwoClass($v['gc_id'], 'gc_id,gc_name', 'gc_sort asc');
            }

        }
        return json_success_return($result);
    }

    #商城--找到每个商品的运营商下面的客服极光id
    public function getOperateImInfo()
    {
        $store_info = db('work_store')->where(['storeid' => get_parent_store_id(), 'status2' => 1])->find();
        return json_success_return(['cliend' => $store_info['store_j_name']]);
    }

    #项目定制--找到项目定制的的后台客服极光
    public function getProjectImInfo()
    {
        $type = input('type','2'); #客服类型 2-项目客服 3-服务客服

        $token = input('token', '');
        $token_info = db('token')->where('token', $token)->find();

        $project_customer = db('work_zi')->where(['typeid' => $type, 'status' => 1])->select();
        $cliend = ''; #极光标识
        foreach ($project_customer as $v) {
            //判断该用户是否在当前项目定制客服分派的用户中，如果再的话直接跳出
            if (strstr(trim($v['userids'], ","), (string)$token_info['user_id'])) {
                $cliend = $v['zi_j_name'];
                break;
            }
            //判断用户地区
            $cliend = $v['zi_j_name'];
        }

        return json_success_return(['cliend' => $cliend]);
    }


}
